/*----------------------------------------------------------------------------
 * Copyright (c) <2016-2020>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/
#include "cJSON.h"

#define CMD_RESPOND 0X30
#define CMD_QUERY 0X10
#define CMD_SET 0X20
#define CMD_BLE 0
#define bufferLen 255
#define oneFrameDataSize 2048
#define BLE_OK 0
#define CMD_SET_DEV 0X09
#define CMD_SET_NET 0X10
#define CMD_QUERY_NET 0X11
#define CMD_QUERY_SN 0X12
#define CMD_QUERY_LOC 0X13
#define CMD_QUERY_DEV 0X14
#define CMD_QUERY_BIND 0X15
#define CMD_QUERY_MODE 0x16

typedef enum {
    BLE_EMGENCY,
    BLE_ERROR,
    BLE_INFO,
    BLE_DEBUG,
    BLE_DETAIL,
}ble_log_level;

extern ble_log_level g_ble_log_level;
#define ble_printf(level, fmat,...) do{if(level <=g_ble_log_level) {printf(fmat,##__VA_ARGS__);}}while(0)

typedef struct{
    unsigned char ble_buffer[bufferLen];
    unsigned short data_head;
    unsigned short data_tail;
    unsigned int data_len;
}ble_rx_buffer;

typedef struct{
    unsigned char cmd;
    unsigned char status;
    unsigned int messageId;
    unsigned int errorCode;
}ble_respond_frame;

typedef struct{
    unsigned char cmd;
    unsigned char sendMode;
    unsigned short crc;
    unsigned int messageId;
    unsigned int dataNumber;
    unsigned char *data;
    unsigned int recvLen;
    unsigned int totalSection;
    unsigned char useSpace;
}ble_set_query_frame;

void ble_receive_byte(unsigned char data);
int  ble_api_send(unsigned int messageId, unsigned char cmd, unsigned char *data, unsigned int len);
int  ble_api_recv(unsigned int *messageId, unsigned char *cmd, unsigned char *data, unsigned int len);
int  ble_init(void);

cJSON *make_ble_respond_json(int iMid,int iStatus,int iErrCode); //构造回复蓝牙模块的JSON数据
int  mcu_general_send_ble(unsigned short iCmd,unsigned char iFormat,unsigned char *pLoadData,unsigned int iDataLen); //向蓝牙发送数据
int  mcu_send_json_ble(unsigned short iCmd,cJSON *pJson,int iFree); //向蓝牙发送JSON数据

int  set_ble_name(char *pSnStr); //设置蓝牙名称
unsigned int ble_set_wifi(unsigned char* data, unsigned int len); //设置WIFI配置
unsigned int ble_get_wifi(unsigned char* data, unsigned int len);	//获取WIFI配置


